package main;

/**
 * 
 * @author Dylan
 * 
 * Control all of the central elements and computing of the game. The heart of the program.
 * 
 */

import java.awt.image.BufferedImage;
import java.util.ArrayList;

import physics.RK4;

public class Model extends Thread{
	protected Controller controller;
	
	public ArrayList<Sprite> sprites;
	public ArrayList<Obj> objects;
	
	RK4 integrator;
	
	double t = 0;	//Game time in seconds
	
	Spider spider;
	Sprite line;
	
	public Model() {
		sprites = new ArrayList<Sprite>();
		objects = new ArrayList<Obj>();
		integrator = new RK4();
		spider = new Spider();
		objects.add(spider);
		sprites.add(spider);
		
		
		BufferedImage img = new BufferedImage(400, 1, BufferedImage.TYPE_INT_RGB);
		line = new Sprite(img, 2);
		line.x = 250;
		line.y = 300;
		sprites.add(line);
	}
	
	public void run(){
		while (true){
			Init.sleep(10);
			
			try{
				//Spider motion - very basic
				if (controller.pressed(65)){
					spider.xVector.a -= 10;
				}
				if (controller.pressed(68)){
					spider.xVector.a += 10;
				}
			}catch(Exception e){}
			
			if (spider.intersects(line)){
				if (spider.y + spider.radius > line.y){
					spider.yVector.a -= .5*(line.y - spider.radius) + spider.yVector.v;
				}
			}
			
			for (Obj object : objects){
				object.move(integrator, t, .1);
			}
			t += .1;
		}
	}
	
	public static Model createModel(Controller controller){
		Model model = new Model();
		model.controller = controller;
		controller.model = model;
		
		model.start();
		return model;
	}
}
